﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation.Forms
{
	public partial class FormulaMethodSubForm : System.Windows.Forms.Form
	{
		public double MaxHeight
		{
			get
			{
				double result;
				try
				{
					result = Convert.ToDouble(this.tbHeight.Text);
				}
				catch (Exception)
				{
					result = 0.0;
				}
				return result;
			}
		}

		public FormulaMethodSubForm(IntPtr parentWndHandle)
		{
			this.m_ParentWndHandle = parentWndHandle;
			this.InitializeComponent();
		}

		public EnvironmentalFactors GetEnvironmentalFactors()
		{
			if (this.rdbWithout.Checked)
			{
				return EnvironmentalFactors.EF_Without;
			}
			if (this.rdbLowerThanOrEqual.Checked)
			{
				return EnvironmentalFactors.EF_LowerThanOrEqual;
			}
			return EnvironmentalFactors.EF_HigherThan;
		}

		public double BuildingLength
		{
			get
			{
				double result;
				try
				{
					result = Convert.ToDouble(this.tbLength.Text);
				}
				catch
				{
					result = 0.0;
				}
				return result;
			}
			set
			{
				this.tbLength.Text = value.ToString("f2");
			}
		}

		public double BuildingWidth
		{
			get
			{
				double result;
				try
				{
					result = Convert.ToDouble(this.tbWidth.Text);
				}
				catch
				{
					result = 0.0;
				}
				return result;
			}
			set
			{
				this.tbWidth.Text = value.ToString("f2");
			}
		}

		public double BuildingHeight
		{
			get
			{
				double result;
				try
				{
					result = Convert.ToDouble(this.tbHeight.Text);
				}
				catch
				{
					result = 0.0;
				}
				return result;
			}
			set
			{
				this.tbHeight.Text = value.ToString("f2");
			}
		}

		private void FormulaMethodSubForm_Load(object sender, EventArgs e)
		{
			this.LoadDlgData();
		}

		private void FormulaMethodSubForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				this.SaveDlgData();
			}
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "FormulaMethodSubForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("rdbWithout", this.rdbWithout.Checked.ToString());
			xmlHelper.SetValue("rdbLowerThanOrEqual", this.rdbLowerThanOrEqual.Checked.ToString());
			xmlHelper.SetValue("rdbHigherThan", this.rdbHigherThan.Checked.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "FormulaMethodSubForm.xml");
			if (!File.Exists(text))
			{
				this.rdbWithout.Checked = true;
				this.m_Length = "0.00";
				this.m_Width = "0.00";
				this.m_Height = "0.00";
				this.tbLength.Text = this.m_Length;
				this.tbWidth.Text = this.m_Width;
				this.tbHeight.Text = this.m_Height;
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			if (xmlHelper.GetValue("rdbWithout") == "True")
			{
				this.rdbWithout.Checked = true;
			}
			else
			{
				this.rdbWithout.Checked = false;
			}
			if (xmlHelper.GetValue("rdbLowerThanOrEqual") == "True")
			{
				this.rdbLowerThanOrEqual.Checked = true;
			}
			else
			{
				this.rdbLowerThanOrEqual.Checked = false;
			}
			if (xmlHelper.GetValue("rdbHigherThan") == "True")
			{
				this.rdbHigherThan.Checked = true;
				return;
			}
			this.rdbHigherThan.Checked = false;
		}

		private void tbLength_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (Geometry.LessThan(Convert.ToDouble(this.tbLength.Text), 0.0))
				{
					this.tbLength.Text = this.m_Length;
				}
				else
				{
					this.m_Length = this.tbLength.Text;
					this.SendUpdateMessageToParent();
				}
			}
			catch (Exception)
			{
				this.tbLength.Text = this.m_Length;
			}
		}

		private void tbWidth_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (Geometry.LessThan(Convert.ToDouble(this.tbWidth.Text), 0.0))
				{
					this.tbWidth.Text = this.m_Width;
				}
				else
				{
					this.m_Width = this.tbWidth.Text;
					this.SendUpdateMessageToParent();
				}
			}
			catch (Exception)
			{
				this.tbWidth.Text = this.m_Width;
			}
		}

		private void tbHeight_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (Geometry.LessThan(Convert.ToDouble(this.tbHeight.Text), 0.0))
				{
					this.tbHeight.Text = this.m_Height;
				}
				else
				{
					this.m_Height = this.tbHeight.Text;
					this.SendUpdateMessageToParent();
				}
			}
			catch (Exception)
			{
				this.tbHeight.Text = this.m_Height;
			}
		}

		[DllImport("User32.dll")]
		private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendUpdateMessageToParent()
		{
			FormulaMethodSubForm.SendMessage(this.m_ParentWndHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void rdbWithout_CheckedChanged(object sender, EventArgs e)
		{
			this.SendUpdateMessageToParent();
		}

		private void rdbLowerThanOrEqual_CheckedChanged(object sender, EventArgs e)
		{
			this.SendUpdateMessageToParent();
		}

		private void rdbHigherThan_CheckedChanged(object sender, EventArgs e)
		{
			this.SendUpdateMessageToParent();
		}

		private const int USER = 1024;

		public const int WM_FICBOX_UPDATE = 1125;

		private IntPtr m_ParentWndHandle;

		private string m_Length = "";

		private string m_Width = "";

		private string m_Height = "";
	}
}
